package cn.com.chec.en.controller.front;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import cn.com.chec.en.dao.ChannelInfoDao;
import cn.com.chec.en.dao.NewsInfoDao;
import cn.com.chec.en.dao.PageLinkDao;
import cn.com.chec.en.model.domain.ChannelInfo;
import cn.com.chec.en.model.domain.QueryNewsObject;
import cn.com.chec.en.model.dto.NewsDTO;

@Controller
public class IndexFrontController {
	protected static final Logger logger = Logger
			.getLogger(IndexFrontController.class);

	@Resource
	private PageLinkDao pageLinkDao;

	@Resource
	private ChannelInfoDao channelInfoDao;

	@Resource
	private NewsInfoDao newsInfoDao;

	@Resource
	private Map<String, Object> environment;

	@RequestMapping(value = { "/index" }, method = { org.springframework.web.bind.annotation.RequestMethod.GET })
	public ModelAndView index(HttpServletRequest request,
			HttpServletResponse response, ModelMap map) throws Exception {
		ModelAndView mav = new ModelAndView("index");

		Map<String, String> pageLinkMap = this.pageLinkDao.getAllAsMap();
		logger.info("[NewsFrontController] pageLinkMap=" + pageLinkMap);
		map.put("pageLinkMap", pageLinkMap);

		QueryNewsObject queryObj = new QueryNewsObject();

		queryObj.setIsFouceImage(1);
		queryObj.setLanguageId(1);
		queryObj.setStart(0);
		queryObj.setNum(6);
		queryObj.setOrderBy(1);
		List<NewsDTO> newsInfoList_pic = this.newsInfoDao
				.getNewsListByQueryChannelId(queryObj, 1);
		map.put("newsInfoList_pic", newsInfoList_pic);

		queryObj = new QueryNewsObject();
		queryObj.setIsTop(1);
		queryObj.setIsShowInIndex(1);
		queryObj.setStart(0);
		queryObj.setNum(6);
		List<NewsDTO> newsInfoTopList = this.newsInfoDao
				.getNewsListByQueryChannelId(queryObj, 1);
		List<NewsDTO> newsInfoNotTopList = new ArrayList<NewsDTO>();
		int topNewsSize = newsInfoTopList != null ? newsInfoTopList.size() : 0;
		if (topNewsSize < 6) {
			queryObj.setIsTop(0);
			queryObj.setStart(0);
			queryObj.setNum(6 - topNewsSize);
			queryObj.setOrderBy(1);
			newsInfoNotTopList = this.newsInfoDao.getNewsListByQueryChannelId(
					queryObj, 1);
		}
		map.put("newsInfoTopList", newsInfoTopList);
		map.put("newsInfoNotTopList", newsInfoNotTopList);

		List<ChannelInfo> busiChannelList = this.channelInfoDao
				.getSubListByParentIdLanguageId(1, 2, 0, 2147483647);
		map.put("busiChannelList", busiChannelList);

		queryObj = new QueryNewsObject();
		queryObj.setLanguageId(1);
		queryObj.setStart(0);
		queryObj.setNum(1);
		NewsDTO aboutCHECDTO = new NewsDTO();
		List<NewsDTO> introductionList = this.newsInfoDao
				.getNewsListByQueryChannelId(queryObj, 27);
		if ((introductionList != null) && (introductionList.size() > 0)) {
			aboutCHECDTO = (NewsDTO) introductionList.get(0);
		}
		String companyIntroduction = "";
		if (aboutCHECDTO.getNewsInfo() != null) {
			companyIntroduction = aboutCHECDTO.getNewsInfo().getContent();
		}
		map.put("companyIntroduction", companyIntroduction);

		queryObj = new QueryNewsObject();
		queryObj.setLanguageId(1);
		queryObj.setIsShowInIndex(1);
		queryObj.setStart(0);
		queryObj.setNum(6);
		List<NewsDTO> newsInfoList_story = this.newsInfoDao
				.getNewsListByQueryPChannelId(queryObj, 4);
		map.put("newsInfoList_story", newsInfoList_story);

		List<ChannelInfo> zhengshuList = this.channelInfoDao
				.getSubListByParentIdLanguageId(1, 31, 0, 2);
		map.put("zhengshuList", zhengshuList);

		queryObj = new QueryNewsObject();
		queryObj.setIsShowInIndex(1);
		queryObj.setLanguageId(1);
		queryObj.setStart(0);
		queryObj.setNum(3);
		List<NewsDTO> zhengshuArtiles = this.newsInfoDao
				.getNewsListByQueryPChannelIdDistinct(queryObj, 31);
		map.put("zhengshuArtiles", zhengshuArtiles);

		map.putAll(this.environment);
		mav.addAllObjects(map);
		return mav;
	}
}